#include "circlegun.h"
#include "camera.h"
#include "bullet.h"
#include "../common.h"

namespace fireplane
{
	CircleGun::CircleGun()
	{}

	CircleGun::~CircleGun()
	{}

	void CircleGun::InitGun(CreateArg* arg){
		CreateNormalGun* tagr = (CreateNormalGun*)arg;

		setPosition(tagr->pos);
		_dir = tagr->tdir;
		_speed = tagr->tspeed;
		_o = tagr->o	;
	}

	void CircleGun::fire(Bullet::BulletImg img)
	{
		CCPoint pos = this->getPosition();
		CCAffineTransform tans = this->getParent()->nodeToWorldTransform();
		pos = CCPointApplyAffineTransform(pos,tans);

		CCPoint dir = Common::CCPointApplyAffineTransform(_dir,tans);

		// fire middle bullet
		dir = ccpNormalize(dir);

		Bullet::BulletCommand* cmd = new Bullet::BulletCommand(Bullet::BT_dir_bullet);
		cmd->_pos = pos;
		cmd->_dir = dir;
		cmd->_speed = _speed;
		cmd->_img = img;
		cmd->o = _o;
		Bullet::pushCommand(cmd);

		float dangle = 10;
		// fire left bullet
		float angle = dangle;
		
		for(int i = 0;i < 3;i++){
			CCPoint tmpDir = dir;
			CCAffineTransform t = CCAffineTransformRotate(CCAffineTransformIdentity,CC_DEGREES_TO_RADIANS(angle));
			tmpDir = Common::CCPointApplyAffineTransform(tmpDir,t);

			Bullet::BulletCommand* cmd = new Bullet::BulletCommand(Bullet::BT_dir_bullet);
			cmd->_pos = pos;
			cmd->_dir = tmpDir;
			cmd->_speed = _speed;
			cmd->_img = img;
			cmd->o = _o;
			Bullet::pushCommand(cmd);

			angle += dangle;
		}

		angle = -dangle;
		for(int i = 0;i < 3;i++){
			CCPoint tmpDir = dir;
			CCAffineTransform t = CCAffineTransformRotate(CCAffineTransformIdentity,CC_DEGREES_TO_RADIANS(angle));
			tmpDir = Common::CCPointApplyAffineTransform(tmpDir,t);

			Bullet::BulletCommand* cmd = new Bullet::BulletCommand(Bullet::BT_dir_bullet);
			cmd->_pos = pos;
			cmd->_dir = tmpDir;
			cmd->_speed = _speed;
			cmd->_img = img;
			cmd->o = _o;
			Bullet::pushCommand(cmd);

			angle -= dangle;
		}
	}
}